package com.example.yy.fragementtest318yy.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.example.yy.fragementtest318yy.R;
import com.example.yy.fragementtest318yy.bean.LoginBean;
import com.example.yy.fragementtest318yy.bean.MyApplication;
import com.example.yy.fragementtest318yy.iface.LoginListener;
import com.example.yy.fragementtest318yy.model.LoginModel;



public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btnr,btnl;
    private EditText eti,etp;
    private MyApplication application;
    private CheckBox cbrem,cbauto;
    private String username="",password="",sessionID="";
    private SharedPreferences sp;
    private LoginListener loginListener=new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            if(loginBean.getId()!=null){
                sessionID=loginBean.getSessionid().toLowerCase();
                saveSP();
                aheadActivity(loginBean);
            }
        }

        @Override
        public void onFail(String msg) {
            System.out.print("fail");
        }
    };

    private void aheadActivity(LoginBean loginBean) {
        Intent intent=new Intent();
        intent.setClass(this,MainActivity.class);
        application = (MyApplication) getApplication();
        application.setSessionid(loginBean.getSessionid());
        startActivity(intent);
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        sp=getSharedPreferences("login",MODE_PRIVATE);
        readSP();
    }

    private void saveSP(){
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putString("sessionID",sessionID);
        editor.putBoolean("remember",cbrem.isChecked());
        editor.commit();
    }
    private void readSP() {
        String name=sp.getString("username",null);
        String pass=sp.getString("password",null);
        Boolean b = sp.getBoolean("remember",false);
        if(b){
            eti.setText(name);
            etp.setText(pass);
            cbrem.setChecked(b);
        }
    }

    private void init() {
        btnl=(Button)findViewById(R.id.button2);
        btnr=(Button)findViewById(R.id.button3);
        eti=(EditText)findViewById(R.id.editText);
        etp=(EditText)findViewById(R.id.editText2);

        cbrem=(CheckBox)findViewById(R.id.checkBox3);
        cbauto=(CheckBox)findViewById(R.id.checkBox2);

        btnl.setOnClickListener(this);
        btnr.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button2:
                LoginModel loginModel=new LoginModel();
                username=eti.getText().toString();
                password=etp.getText().toString();
                loginModel.getResult(eti.getText().toString(),etp.getText().toString(),loginListener);
                System.out.println();
                break;
            case R.id.button3:
                Intent intent=new Intent();
                intent.setClass(this,RegisterActivity.class);
                startActivity(intent);
                break;
        }

    }
}
